//
//  CharacterCreateStepViewController.swift
//  SkylowAPP
//
//  Created by 青色石头 on 2024/9/21.
//

import UIKit
import SnapKit

class CharacterCreateStepViewController: UIViewController {
    
    private lazy var stepHeader = {
        let step = CharacterStepView()
        self.view.addSubview(step)
        step.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            step.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            step.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            step.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
            step.heightAnchor.constraint(equalToConstant: 72 / 430.0 * kScreenWidth)
        ])
        return step
    }()
    
    private lazy var commitBtn = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(UIImage(named: "character_submit_icon"), for: .normal)
        btn.contentMode = .scaleAspectFill
        btn.addTarget(self, action: #selector(continueAction), for: .touchUpInside)
        btn.setTitle("Continue", for: .normal)
        btn.titleLabel?.font = .medium(16)
        self.view.addSubview(btn)
        btn.snp.makeConstraints { make in
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-12)
            make.centerX.equalToSuperview()
            make.height.equalTo(32)
            make.width.equalToSuperview().multipliedBy(398 / 430.0)
        }
        return btn
    }()
    
    private lazy var contentView = {
        let vw = UIView()
        vw.backgroundColor = .black
        self.view.addSubview(vw)
        vw.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(stepHeader.snp.bottom)
            make.bottom.equalTo(commitBtn.snp.top)
        }
        return vw
    }()
    
    private lazy var avatarContentView = {
        let vc = CharacterStepAvatarViewController()
        return vc
    }()
    
    private lazy var definitionContentView = {
        let vc = CharacterStepDefinitionViewController()
        return vc
    }()
    
    private lazy var publishContentView = {
        let vc = CharacterStepPublishViewController()
        return vc
    }()
    
    var step: Int = 0 {
        didSet {
            stepHeader.currentStep = step
        }
    }
    
    private var currentContentVC: UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
        configNav()
        _ = stepHeader
        _ = commitBtn
        _ = contentView
        step = 0
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.standardAppearance.backgroundImage = .navBackgroundImage
        navigationController?.navigationBar.scrollEdgeAppearance?.backgroundImage = .navBackgroundImage
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if currentContentVC == nil {
            switchContent(withStep: step, isBack: false)
        }
    }

}

fileprivate extension CharacterCreateStepViewController {
    
    func configNav() {
        
        navigationItem.title = "Avatar"
        let closeItem = UIBarButtonItem(image: UIImage(named: "character_close_icon")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(closeAction))
        navigationItem.leftBarButtonItem = closeItem
        
        let backBtn = UIButton(type: .custom)
        backBtn.titleLabel?.font = .systemFont(ofSize: 14)
        backBtn.setTitle("Back", for: .normal)
        backBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        backBtn.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        NSLayoutConstraint.activate([
            backBtn.heightAnchor.constraint(equalToConstant: 36)
        ])
        let backItem = UIBarButtonItem(customView: backBtn)
        navigationItem.rightBarButtonItem = backItem
    }
    @objc func closeAction() {
        dismiss(animated: true)
    }
    
    @objc func backAction() {
        if step <= 0 {
            return
        }
        step = step - 1
        switchContent(withStep: step, isBack: true)
    }
    
    @objc func continueAction() {
        if step >= 3 {
            EWToast.showCenterWithText(text: "Submit", duration: 2)
            dismiss(animated: true)
            return
        }
        step = step + 1
        switchContent(withStep: step, isBack: false)
    }
}

extension CharacterCreateStepViewController {
    
    func switchContent(withStep step: Int, isBack: Bool) {
        var willShowContentVC: UIViewController? = nil;
        if step == 0 || step == 1 {
            commitBtn.setTitle("Continue", for: .normal)
            willShowContentVC = avatarContentView
        }else if step == 2 {
            willShowContentVC = definitionContentView
            commitBtn.setTitle("Continue", for: .normal)
        }else {
            willShowContentVC = publishContentView
            commitBtn.setTitle("Submit", for: .normal)
        }
        guard let willShowContentVC else { return }
        
        if willShowContentVC === currentContentVC {
            return
        }
        
        if currentContentVC == nil {
            addChild(willShowContentVC)
            willShowContentVC.didMove(toParent: self)
            
            willShowContentVC.beginAppearanceTransition(true, animated: false)
            self.contentView.addSubview(willShowContentVC.view)
            willShowContentVC.view.frame = contentView.bounds
            willShowContentVC.endAppearanceTransition()
            currentContentVC = willShowContentVC
            return
        }
        guard let currentContentVC else { return }
        self.currentContentVC = willShowContentVC
        
        addChild(willShowContentVC)
        willShowContentVC.didMove(toParent: self)
        
        willShowContentVC.beginAppearanceTransition(true, animated: false)
        self.contentView.addSubview(willShowContentVC.view)
        willShowContentVC.view.frame = contentView.bounds
        
        willShowContentVC.view.transform = CGAffineTransformMakeTranslation(isBack ? -contentView.width : contentView.width, 0)
        transition(from: currentContentVC, to: willShowContentVC, duration: 0.25) {
            currentContentVC.view.transform = CGAffineTransformMakeTranslation(isBack ? self.contentView.width : -self.contentView.width, 0)
            willShowContentVC.view.transform = CGAffineTransform.identity
        } completion: { _ in
            currentContentVC.view.transform = CGAffineTransform.identity
            currentContentVC.view.removeFromSuperview()
            currentContentVC.removeFromParent()
            willShowContentVC.endAppearanceTransition()
        }
        
    }
}
